package com.lemon.springboot.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
 * @Author liuli
 * @Date 2020/12/21 13:54
 */
public class MD5Util {

    public static String getMD5(String info) {
        StringBuilder strBuilder = new StringBuilder();
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(info.getBytes("UTF-8"));
            byte[] md5Array = md5.digest();
            for (int i = 0; i < md5Array.length; i++) {
                int temp = 0xff & md5Array[i];
                String hexString = Integer.toHexString(temp);
                if (hexString.length() == 1) {
                    strBuilder.append("0").append(hexString);
                } else {
                    strBuilder.append(hexString);
                }
            }
        } catch (NoSuchAlgorithmException e) {
            return info;
        } catch (UnsupportedEncodingException e) {
            return info;
        }
        return strBuilder.toString();
    }

    public static String getUUIDToMD5(){
        return getMD5(UUID.randomUUID().toString());
    }
}
